//
//  LYMessageTableViewCell.swift
//  WeChat
//
//  Created by mac on 2018/7/26.
//  Copyright © 2018年 RRC. All rights reserved.
//

import UIKit

class LYMessageTableViewCell: UITableViewCell {
    
    var avatarImageView:UIImageView?
    var unreadNumberlabel:UILabel?
    var dateLabel:UILabel?
    var nameLabel:UILabel?
    var lastMessageLabel:UILabel?
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        avatarImageView = UIImageView()
        self.addSubview(avatarImageView!)
        avatarImageView?.snp.makeConstraints({ (make) in
            make.top.equalTo(8)
            make.leading.equalTo(10)
            make.width.height.equalTo(48)
        })
        
        nameLabel = UILabel()
        nameLabel?.text = "你好"
        nameLabel?.font = UIFont.systemFont(ofSize: 16)
        nameLabel?.textColor = UIColor.init(ts_hexString: "#262626")
        nameLabel?.textAlignment = NSTextAlignment.left
        self.addSubview(nameLabel!)
        nameLabel?.snp.makeConstraints({ (make) in
            make.top.equalTo(4)
            make.height.equalTo(20)
            make.leading.equalTo(avatarImageView!.snp.trailing).offset(12)
            make.width.greaterThanOrEqualTo(32)
        })
        
        lastMessageLabel = UILabel()
        lastMessageLabel?.text = "消息内容"
        lastMessageLabel?.font = UIFont.systemFont(ofSize: 14)
        lastMessageLabel?.textColor = UIColor.init(ts_hexString: "#8a8a8a")
        lastMessageLabel?.textAlignment = NSTextAlignment.left
        self.addSubview(lastMessageLabel!)
        lastMessageLabel?.snp.makeConstraints({ (make) in
            make.leading.equalTo(avatarImageView!.snp.trailing).offset(12)
            make.trailing.equalTo(-2)
            make.bottom.equalTo(-2)
            make.height.equalTo(17)
        })
        
        dateLabel = UILabel()
        dateLabel?.text = "14-01-20"
        dateLabel?.font = UIFont.systemFont(ofSize: 13)
        dateLabel?.textColor = UIColor.init(ts_hexString: "#8c8c8c")
        dateLabel?.textAlignment = NSTextAlignment.right
        self.addSubview(dateLabel!)
        dateLabel?.snp.makeConstraints({ (make) in
            make.trailing.equalTo(0)
            make.width.greaterThanOrEqualTo(61)
            make.height.equalTo(14)
            make.leading.equalTo(nameLabel!.snp.trailing).offset(27)
        })
        
        unreadNumberlabel = UILabel()
        unreadNumberlabel?.text = "99"
        unreadNumberlabel?.backgroundColor = UIColor.red
        unreadNumberlabel?.font = UIFont.systemFont(ofSize: 11)
        unreadNumberlabel?.textColor = UIColor.white
        unreadNumberlabel?.textAlignment = NSTextAlignment.center
        self.addSubview(unreadNumberlabel!)
        unreadNumberlabel?.snp.makeConstraints({ (make) in
            make.leading.equalTo(45)
            make.top.equalTo(2)
            make.height.equalTo(18)
            make.width.greaterThanOrEqualTo(18)
        })
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func setNeedsLayout() {
        self.avatarImageView?.layer.masksToBounds = true
        self.avatarImageView?.layer.cornerRadius = avatarImageView!.width / 2 / 180*30
        
        self.unreadNumberlabel?.layer.masksToBounds = true
        self.unreadNumberlabel?.layer.cornerRadius = unreadNumberlabel!.height / 2
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        
        
    }
    
    
    func setCellContent(_ model:LYMessageModel){
        self.avatarImageView?.ly_setImageWithURLString(model.middleImageURL, placeholderImage: model.messageFromType.placeHolderImage)
        self.unreadNumberlabel?.text = model.unreadNumber! > 99 ? "99+" : String(model.unreadNumber!)
        self.unreadNumberlabel?.isHidden = (model.unreadNumber == 0)
        self.lastMessageLabel?.text = model.lastMessage!
        self.dateLabel?.text = model.dateString!
        self.nameLabel?.text = model.nickName!
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}
